package com.abu.crms.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.PageUtil;
import com.abu.crms.config.common.Resp;
import com.abu.crms.config.exception.CRException;
import com.abu.crms.pojo.Role;
import com.abu.crms.service.RoleService;
import com.abu.crms.utils.PageUtils;
import com.abu.crms.vo.RoleSelectVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/crms/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("queryRoleListInfo")
    public Resp queryRoleInfoList() {
        List<Role> roles = this.roleService.list();
        List<RoleSelectVO> roleSelectVOList = roles.stream().map(role -> {
            RoleSelectVO roleSelectVO = new RoleSelectVO();
            roleSelectVO.setRoleId(role.getRoleId()).setRoleName(role.getRoleName());
            return roleSelectVO;
        }).collect(Collectors.toList());
        return Resp.ok().data("roleSelectVOList", roleSelectVOList);
    }

    @GetMapping("getRoleListPage")
    public Resp queryByPage(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "limit", defaultValue = "10") Long limit
    ) {
//        构造分页条件
        Page<Role> pageResult = this.roleService.page(new Page<>(page, limit));
        List<Role> records = pageResult.getRecords();
        if (CollectionUtil.isEmpty(records)) {
            throw new CRException("数据为空");
        }
//        封装结果集
        PageUtils pageUtilsResult = new PageUtils(pageResult);
        return Resp.ok().data("pageResult", pageUtilsResult);
    }


    @PutMapping("updateRoleInfo")
    public Resp updateRoleInfo(@RequestBody Role role) {
        boolean flag = this.roleService.updateById(role);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("修改失败");
    }

    @PostMapping("addRole")
    public Resp addRole(@RequestBody Role role) {
        String roleName = role.getRoleName();
        Integer roleLevel = role.getRoleLevel();
        Role getRole = this.roleService.getOne(new QueryWrapper<Role>()
                .eq(" role_name", roleName)
                .eq("role_level", roleLevel));
        if (getRole != null) {
            throw new CRException("该角色名已经存在了，请更换一个");
        }
        boolean flag = this.roleService.save(role);
        if (flag) {
            return Resp.ok().msg("添加成功");
        }
        return Resp.error().msg("添加失败");
    }

    @DeleteMapping("deleteRoleByRoleId/{roleId}")
    public Resp deleteRoleByRoleId(@PathVariable("roleId") Long roleId) {
        Boolean flag= this.roleService.removeRoleAndRolePermission(roleId);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("删除失败");
    }


}

